<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div class="search" v-show="showSearch">
        <el-form :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
          <el-form-item label="场地" prop="positionId">
            <el-select v-model="queryParams.positionId" placeholder="请选择" @keyup.enter="handleQuery" clearable="true">
              <el-option
                v-for="item in positionList"
                :key="item.positionId"
                :label="item.name"
                :value="item.positionId"
                :disabled="item.status == 'discard'"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="openId" prop="openid">
            <el-input v-model="queryParams.openid" placeholder="请输入openId" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="订单编号" prop="orderNo">
            <el-input v-model="queryParams.orderNo" placeholder="请输入订单编号" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="分账单号" prop="outOrderNo">
            <el-input v-model="queryParams.outOrderNo" placeholder="请输入分账单号" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="状态" prop="state">
            <el-select v-model="queryParams.state" placeholder="请选择状态" clearable>
              <el-option
                v-for="dict in pay_split_state"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="paySplitList" border="true">
        <el-table-column type="index" label="#" width="55" align="center" fixed="left"/>
        <el-table-column label="ID" align="center" prop="paySplitId" v-if="false" />
        <el-table-column label="场地ID" align="center" prop="positionId" v-if="false"/>
        <el-table-column label="场地名称" align="center" prop="positionName" />
        <el-table-column label="管理员ID" align="center" prop="managerId" v-if="false"/>
        <el-table-column label="管理员名称" align="center" prop="managerName" />
        <el-table-column label="openId" align="center" prop="openid" />
        <el-table-column label="订单编号" align="center" prop="orderNo" />
        <el-table-column label="流水号" align="center" prop="transactionNo" />
        <el-table-column label="分账单号" align="center" prop="outOrderNo" />
        <el-table-column label="分账描述" align="center" prop="description" />
        <el-table-column label="订单金额" align="center" prop="orderAmount" />
        <el-table-column label="分账金额" align="center" prop="splitAmount" />
        <el-table-column label="状态" align="center" prop="state">
          <template #default="scope">
            <dict-tag :options="pay_split_state" :value="scope.row.state"/>
          </template>
        </el-table-column>
        <el-table-column label="操作时间" align="center" prop="createTime" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
<!--        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
<!--          <template #default="scope">-->
<!--            <el-tooltip content="修改" placement="top">-->
<!--              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['device:paySplit:edit']"></el-button>-->
<!--            </el-tooltip>-->
<!--            <el-tooltip content="删除" placement="top">-->
<!--              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['device:paySplit:remove']"></el-button>-->
<!--            </el-tooltip>-->
<!--          </template>-->
<!--        </el-table-column>-->
      </el-table>

      <pagination
          v-show="total>0"
          :total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
      />
    </el-card>
    <!-- 添加或修改微信分账对话框 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="500px" append-to-body>
      <el-form ref="paySplitFormRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="场地ID" prop="positionId">
          <el-input v-model="form.positionId" placeholder="请输入场地ID" />
        </el-form-item>
        <el-form-item label="管理员ID" prop="managerId">
          <el-input v-model="form.managerId" placeholder="请输入管理员ID" />
        </el-form-item>
        <el-form-item label="openId" prop="openid">
          <el-input v-model="form.openid" placeholder="请输入openId" />
        </el-form-item>
        <el-form-item label="订单编号" prop="orderNo">
          <el-input v-model="form.orderNo" placeholder="请输入订单编号" />
        </el-form-item>
        <el-form-item label="流水号" prop="transactionNo">
          <el-input v-model="form.transactionNo" placeholder="请输入流水号" />
        </el-form-item>
        <el-form-item label="分账单号" prop="outOrderNo">
          <el-input v-model="form.outOrderNo" placeholder="请输入分账单号" />
        </el-form-item>
        <el-form-item label="分账描述" prop="description">
          <el-input v-model="form.description" placeholder="请输入分账描述" />
        </el-form-item>
        <el-form-item label="订单金额" prop="orderAmount">
          <el-input v-model="form.orderAmount" placeholder="请输入订单金额" />
        </el-form-item>
        <el-form-item label="分账金额" prop="splitAmount">
          <el-input v-model="form.splitAmount" placeholder="请输入分账金额" />
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-select v-model="form.state" placeholder="请选择状态">
            <el-option
                v-for="dict in pay_split_state"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="PaySplit" lang="ts">
import { listPaySplit, getPaySplit, delPaySplit, addPaySplit, updatePaySplit } from '@/api/device/paySplit';
import { PaySplitVO, PaySplitQuery, PaySplitForm } from '@/api/device/paySplit/types';
import {allPosition} from "@/api/device/position";

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { pay_split_state } = toRefs<any>(proxy?.useDict('pay_split_state'));

const paySplitList = ref<PaySplitVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const paySplitFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: PaySplitForm = {
  paySplitId: undefined,
  positionId: undefined,
  managerId: undefined,
  openid: undefined,
  orderNo: undefined,
  transactionNo: undefined,
  outOrderNo: undefined,
  description: undefined,
  orderAmount: undefined,
  splitAmount: undefined,
  state: undefined,
}
const data = reactive<PageData<PaySplitForm, PaySplitQuery>>({
  form: {...initFormData},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    orderByColumn: 'create_time',
    isAsc: 'desc',
    positionId: undefined,
    openid: undefined,
    orderNo: undefined,
    outOrderNo: undefined,
    state: undefined,
    createTime: undefined,
    params: {
    }
  },
  positionList:[],
  rules: {
    paySplitId: [
      { required: true, message: "ID不能为空", trigger: "blur" }
    ],
    positionId: [
      { required: true, message: "场地ID不能为空", trigger: "blur" }
    ],
    managerId: [
      { required: true, message: "管理员ID不能为空", trigger: "blur" }
    ],
    openid: [
      { required: true, message: "openId不能为空", trigger: "blur" }
    ],
    orderNo: [
      { required: true, message: "订单编号不能为空", trigger: "blur" }
    ],
    transactionNo: [
      { required: true, message: "流水号不能为空", trigger: "blur" }
    ],
    outOrderNo: [
      { required: true, message: "分账单号不能为空", trigger: "blur" }
    ],
    description: [
      { required: true, message: "分账描述不能为空", trigger: "blur" }
    ],
    orderAmount: [
      { required: true, message: "订单金额不能为空", trigger: "blur" }
    ],
    splitAmount: [
      { required: true, message: "分账金额不能为空", trigger: "blur" }
    ],
    state: [
      { required: true, message: "状态不能为空", trigger: "change" }
    ],
  }
});

const { queryParams, form, rules,  positionList } = toRefs(data);

/** 查询微信分账列表 */
const getList = async () => {
  loading.value = true;
  const res = await listPaySplit(queryParams.value);
  paySplitList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
}

/** 表单重置 */
const reset = () => {
  form.value = {...initFormData};
  paySplitFormRef.value?.resetFields();
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

/** 多选框选中数据 */
const handleSelectionChange = (selection: PaySplitVO[]) => {
  ids.value = selection.map(item => item.paySplitId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = "添加微信分账";
}

/** 修改按钮操作 */
const handleUpdate = async (row?: PaySplitVO) => {
  reset();
  const _paySplitId = row?.paySplitId || ids.value[0]
  const res = await getPaySplit(_paySplitId);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = "修改微信分账";
}

/** 提交按钮 */
const submitForm = () => {
  paySplitFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.paySplitId) {
        await updatePaySplit(form.value).finally(() =>  buttonLoading.value = false);
      } else {
        await addPaySplit(form.value).finally(() =>  buttonLoading.value = false);
      }
      proxy?.$modal.msgSuccess("修改成功");
      dialog.visible = false;
      await getList();
    }
  });
}

/** 删除按钮操作 */
const handleDelete = async (row?: PaySplitVO) => {
  const _paySplitIds = row?.paySplitId || ids.value;
  await proxy?.$modal.confirm('是否确认删除微信分账编号为"' + _paySplitIds + '"的数据项？').finally(() => loading.value = false);
  await delPaySplit(_paySplitIds);
  proxy?.$modal.msgSuccess("删除成功");
  await getList();
}

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download('device/paySplit/export', {
    ...queryParams.value
  }, `paySplit_${new Date().getTime()}.xlsx`)
}

const getPositionList = async () => {
  loading.value = true;
  const res = await allPosition();
  positionList.value = res.data;
  loading.value = false;
}

onMounted(() => {
  getList();
  getPositionList();
});
</script>
